# -*- coding: utf-8 -*-
import numpy as np
import pandas as pd

def return_data(data, sensor_name):
    '''
    
    '''
    history = [data.iloc[:,-i].values for i in [5,4,3,2,1]]
    score_predict = []
    for i in range(4):
        predict = np.mean(history, axis=0)
        score_predict.append(predict)
        history.pop(0)
        history.append(predict)
    
    score_predict = np.array(score_predict).T
    score_predict = pd.DataFrame(data=score_predict)
    
    accident_score = score_predict.sum(axis=0)
    
    idx_list = ['23:15', '23:30', '23:45', '24:00']
    sensor_list = {}
    for i in range(4):
        # 4 个时刻
        tmp = score_predict.iloc[:, i]
        sensor_list_period = []
        for j in range(1, 6):
            # 5 个感应器
            data_sort = np.sort(tmp)[-j]
            if data_sort >= 0.99 and data_sort <= 1.01:
                continue
            idx = np.where(tmp == data_sort)[0][0]
            tmp[idx] = 0
            sensor_worst = sensor_name[idx]
            sensor_list_period.append(sensor_worst)
        
        sensor_list_period.append(accident_score[i])
        sensor_list[idx_list[i]] = sensor_list_period
    
    return accident_score, sensor_list

def print_result(sensor_list):
    '''
    打印结果
    '''
    
    for k,v in sensor_list.items():
        print(f'当前时刻为：{k}')
        print('异常的感应器有： ', v[:-1])
        print('异常得分为： ', v[-1], '\n')
        
if __name__ == '__main__':
    path = r'../附件/中间数据/时间-风险-15分钟.xlsx'
    data = pd.read_excel(path, index_col=0, header=0)
    sensor_name = data.index
    
    sensor_list = return_data(data, sensor_name)
    print_result(sensor_list)
    
